# -*- coding:utf-8 -*-
from tornado import gen
from tornado.ioloop import IOLoop
from appcommon.httputil import get_request_data
import urllib


url_access_token = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=wxce3ced320373d100&secret=7876e69a2f28e791f2c8f7e89a015ecc"

@gen.coroutine
def get_access_token():
    res = yield get_request_data(object, url_access_token)
    raise gen.Return(res)


@gen.coroutine
def get_sucai_list(token):
    url = "https://api.weixin.qq.com/cgi-bin/material/batchget_material?access_token={}".format(token)
    param = dict(
        type="news",
        offset=0,
        count=10
    )
    res = yield get_request_data(object, url, method='POST', para_dict=param)
    raise gen.Return(res)


@gen.coroutine
def get_menu(token):
    url = "https://api.weixin.qq.com/cgi-bin/menu/get?access_token={}".format(token)
    res = yield get_request_data(object, url)
    raise gen.Return(res)


BASE_URL = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=wxce3ced320373d100&redirect_uri={}&response_type=code&scope=snsapi_base&state=123&connect_redirect=1#wechat_redirect"

@gen.coroutine
def update_menu(token):
    # param = {
    #     "button": [
    #         {
    #             "type": "view",
    #             "name": u"启程app",
    #             "url": "http://www.qcjkjg.com/news"
    #         },
    #         {
    #             "type": "media_id",
    #             "name": u"驾考天地",
    #             "media_id": "Z00aqOU9zQC3rx72XzG48jQRVNm-nmAjuNz_UaK_Iv4"
    #         },
    #         {
    #             "type": "view",
    #             "name": u"商务合作",
    #             "url": "http://www.qcjkjg.com/"
    #         }
    #     ]
    # }

    param = {
        "button": [
            {
                "name": u"我是学员",
                "sub_button":
                [
                    {
                        "type": "view",
                        "name": u"开通科一VIP",
                        "url": BASE_URL.format(urllib.quote("http://app.qcjkjg.com/static/html/instructor/wechat_invitation.html?order_type=vip1", safe=''))
                    },
                    {
                        "type": "view",
                        "name": u"开通科四VIP",
                        "url": BASE_URL.format(urllib.quote("http://app.qcjkjg.com/static/html/instructor/wechat_invitation.html?order_type=vip4",safe=''))
                    },
                    {
                        "type": "view",
                        "name": u"邀请好友",
                        # "url": BASE_URL.format(urllib.quote("http://app.qcjkjg.com/static/html/instructor/share_code.html", safe=''))
                        "url": BASE_URL.format(urllib.quote("http://app.qcjkjg.com/wechatshare", safe=''))
                    },
                    # {
                    #     "type": "view",
                    #     "name": u"下载客户端",
                    #     "url": "http://www.qcjkjg.com/"
                    # }
                ]
            },
            {
                "name": u"我是教练",
                "sub_button":
                    [
                        {
                            "type": "view",
                            "name": u"免费制作名片",
                            "url": BASE_URL.format(urllib.quote("http://app.qcjkjg.com/static/html/instructor/card.html?from=wechat", safe=''))
                        },
                        {
                            "type": "view",
                            "name": u"邀请学员",
                            # "url": BASE_URL.format(urllib.quote("http://app.qcjkjg.com/static/html/instructor/share_code.html", safe=''))
                            # "url": BASE_URL.format(urllib.quote("http://app.qcjkjg.com/static/html/invitation_download.html?from=wechat&share_from=wechat", safe=''))
                            "url": BASE_URL.format(urllib.quote("http://app.qcjkjg.com/wechatshare", safe=''))
                        },
                        {
                            "type": "view",
                            "name": u"认证入驻",
                            "url": BASE_URL.format(urllib.quote("http://app.qcjkjg.com/static/html/instructor/authentication.html?from=wechat", safe=''))
                        }
                    ]
            },
            {
                "name": u"关于启程",
                "sub_button":
                    [
                        {
                            "type": "view",
                            "name": u"关于启程",
                            "url": "http://app.qcjkjg.com/static/html/about.html"
                        },
                        {
                            "type": "view",
                            "name": u"下载客户端",
                            "url": "http://app.qcjkjg.com/static/html/download.html"
                        },
                        {
                            "type": "view",
                            "name": u"绑定手机",
                            "url": BASE_URL.format(urllib.quote(
                                "http://app.qcjkjg.com/static/html/instructor/auth_check.html",
                                safe=''))
                        }

                        # {
                        #     "type": "view",
                        #     "name": u"奖金提现",
                        #     "url": "http://app.qcjkjg.com/static/html/invitation_download.html?phone_code=xxxxxx"
                        # },
                        # {
                        #     "type": "view",
                        #     "name": u"分享有奖",
                        #     "url": BASE_URL.format(urllib.quote("http://app.qcjkjg.com/static/html/invitation_download.html?from=wechat&share_from=wechat", safe=''))
                        # }
                    ]
            }
        ]
    }
    url = "https://api.weixin.qq.com/cgi-bin/menu/create?access_token={}".format(token)
    res = yield get_request_data(object, url, method='POST', para_dict=param)
    raise gen.Return(res)


@gen.coroutine
def main():
    res = yield get_access_token()
    print res
    token = res['access_token']

    res = yield update_menu(token)
    print res
    menu_list = yield get_menu(token)
    print menu_list


if __name__ == "__main__":
    IOLoop.current().run_sync(main)